import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login'
import Home from '../views/Home'
import WelCome from '../views/WelCome'
import users from '../views/users'
import categories from '../views/categories'
import goods from '../views/goods'
import orders from '../views/orders'
import params from '../views/params'
import reports from '../views/reports'
import rights from '../views/rights'
import roles from '../views/roles'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  {
    path: '/login',
    component: Login,

  },
  {
    name: 'home',
    path: '/home',
    component: Home,
    redirect: '/home/WelCome',
    meta: { isAuth: true },
    children:[
        {
          path:'WelCome',
          component: WelCome,
          meta: { isAuth: true },
        },
        {
          path:'users',
          component: users,
          meta: { isAuth: true },
        },
        {
          path:'categories',
          component: categories,
          meta: { isAuth: true },
        },
        {
          path:'goods',
          component: goods,
          meta: { isAuth: true },
        },
        {
          path:'orders',
          component: orders,
          meta: { isAuth: true },
        },
        {
          path:'params',
          component: params,
          meta: { isAuth: true },
        },
        {
          path:'reports',
          component: reports,
          meta: { isAuth: true },
        },
        {
          path:'rights',
          component: rights,
          meta: { isAuth: true },
        },
        {
          path:'roles',
          component: roles,
          meta: { isAuth: true },
        },
    ]
  }
]

const router = new VueRouter({
  routes
})

// 全局前置守卫：初始化时执行、每次路由切换前执行
router.beforeEach((to, from, next) => {
  // console.log('beforeEach', to, from)
  if (to.meta.isAuth) { //判断当前路由是否需要进行权限控制
    if (sessionStorage.getItem('token')) { //权限控制的具体规则
      next() //放行
    } else {
      alert('对不起你没有权限访问，请先登录')
      next('/login')
      // next({name:'guanyu'})
    }
  } else {
    next() //放行
  }
  // console.log(sessionStorage.getItem('token'));
  next() //放行
})

export default router
